/*
 * 用户管理模块
 */
ArchivingConfigCurd = Ext.extend(RockyGridCurd,{
	win:null,
	fp : null,
	baseUrl: 'archivingConfig.action',
	winCfg : {
		maximizable:true, 
		width:310,
		title : "归档时间设置"
	},	
	title:"归档时间设置",	
	trackMouseOver : false,
	loadMask:false, 
	autoWidth : false,		
	autoScroll:true,
	closable : true,	
	viewConfig : {
		forceFit : false
	},	
	constructor : function(config) {	
		var store = new Ext.data.JsonStore( {		
			id : "id",
			url : 'archivingConfig.action?cmd=read',
			root : "data",
			totalProperty : "rowCount",
			remoteSort : false,
			fields : ["createTime","creator","deleted","description","endTime","id","keyword","memo","modifier","modifyIp","modifyTime","showindex","startTime","year"],
			listeners:{
				beforeload: function() {					
					store.baseParams.keywordFilterValue ='';				
					store.baseParams.roleFilterValue ='';					
				},scope:this
			}
		});
		store.baseParams.limit=20;
		this.keywordFilter = new Ext.form.TextField( {
								width : 286,
								pressed : true,
								scope : this
		});
		var sm = new Ext.grid.CheckboxSelectionModel();	
		var cm = new Ext.grid.ColumnModel([ sm,new Ext.grid.RowNumberer(),{
				header : "年份",
				sortable : false,
				width:200,
				dataIndex : "year"
			}, {
				header : "开始日期",
				sortable : true,width:200,				
				dataIndex : "startTime"
			}, {		
				header : "截止日期",
				sortable : true,width:200,				
				dataIndex : "endTime"			
			}, {
				header : "实际存档时间",
				sortable : true,width:200,				
				dataIndex : "realTime"
			}, {
				header : "存档状态",
				sortable : true,width:100,	
				renderer:function(v){
					return !!v;
				},
				dataIndex : "realTime"	
			}]);
		cm.defaultSortable = true;
		Ext.apply(this, {
			cm :cm,
			store :  store,
			sm: sm,
			tbar : ['   ', {
				text : '添加',
				iconCls:'add',
				pressed : false,
				handler : this.create,
				scope : this
			}, '   ', {
				text : '修改',
				iconCls:'edit',			
				pressed : false,
				handler : this.edit,
				scope : this
			}, '   ', {
				text : '删除',
				iconCls:'remove',
				pressed : false,
				handler : this.removeData,
				scope : this
			}, '   ', {
				text : '刷新',
				iconCls:'refresh',
				pressed : false,
				handler : this.refresh,
				scope : this
			}, new Ext.Toolbar.Fill()
			],			
			bbar : new Ext.PagingToolbar( {
				pageSize : 20,
				store : store,
				displayInfo : true,
				displayMsg : '显示{0}-{1}，共{2}条记录',
				emptyMsg : "没有可显示的记录！"
			})
		});
		ArchivingConfigCurd.superclass.constructor.call(this, config);	
	},
	save : function() {
		if(this.fp.form.isValid()){
			var year = this.fp.find('name',"year")[0];
			var startTime = this.fp.find('name',"startTime")[0];	
			var endTime = this.fp.find('name',"endTime")[0];
			//alert(year.parseDate(endTime.getValue().getFullYear()+"-01-01", "Y-m-d").format('Y-m-d'));
			//alert(startTime.getValue().format('Y-m-d'));
			//alert(Date.parseDate(endTime.getValue().getFullYear()+"-01-01", "Y-m-d").format('Y-m-d'));
			//alert(Date.parseDate((new Date(year.getValue())), "Y"));
			var smllDate = Date.parseDate(year.getValue().getFullYear()+"-01-01", "Y-m-d");
			var largeDate = Date.parseDate(year.getValue().getFullYear()+"-12-31", "Y-m-d");
			if(startTime.getValue()<smllDate){
				Ext.Msg.alert("提示","起始日期必须在"+year.getValue().getFullYear()+"年内，请纠正！")
				return false;
			}
			if(endTime.getValue()>largeDate){
				Ext.Msg.alert("提示","截止日期必须在"+year.getValue().getFullYear()+"年内，请纠正！")
				return false;
			}
			if(startTime.getValue()>endTime.getValue()){
				Ext.Msg.alert("提示","起始日期不能大于截止日期，请纠正！")
				return false;
			}
//			return alert("验证通过");
			var id = this.fp.find("name","id")[0].getValue();
			var formData = this.fp.form.getValues();
			var jsondata = Ext.util.JSON.encode(formData);
			Ext.Ajax.request( {
				url : this.baseUrl,
				params : {	
					'cmd':(id ? "update" : "create"),				
					'jsondata':jsondata
				},
				method : 'POST',
				success : function(response) {				
					var r = Ext.util.JSON.decode(response.responseText);
					if (!r.success) {
						Ext.Msg.alert("提示信息","失败，由以下原因所致：<br/>"+ (r.errors? r.errors: "未知原因"));
					} else {
						Ext.Msg.alert("提示信息", "保存成功!",function(){
							this.closeWin();
							this.refresh();
						},this);

					}
				},
				failure:function(Optional){				
					Ext.Msg.alert("提示信息", "连接服务器失败!");								 
				}, 
				scope : this
			});
		}
	},	
	createForm : function() {
		var formPanel = new Ext.form.FormPanel( {
		  	autoScroll:true,
			frame : true,
			border:false,			
			bodyStyle : 'position:relative;margin:15px 0 0px 15px',
			labelAlign : 'left',
			width : 300,
			height : 100,
			items : [{
					xtype : "hidden",
					name : "id"
            },{
				xtype : 'datefield',
				format : 'Y',
				editable : true,
				fieldLabel : '年份*',
				allowBlank : false,
				readOnly : true,
				name : 'year',
				anchor : '95%'
            },{  
				xtype : 'datefield',
				format : 'Y-m-d',
				editable : true,
				fieldLabel : '开始日期*',
				allowBlank : false,
				readOnly : true,
				name : 'startTime',
				anchor : '95%'
	        },{   
				xtype : 'datefield',
				format : 'Y-m-d',
				editable : true,
				fieldLabel : '截止日期*',
				allowBlank : false,
				readOnly : true,
				name : 'endTime',
				anchor : '95%'
	        }]
		});
		return formPanel;
	}
});